<!--#include file="./header.inc"-->
<%

select case aq
	'link
	case "save" 		: 	call save
	case "edit" 		: 	call edit
	case "delete" 		: 	call delete
	case "editbatch" 	: 	call editBatch
	case "savebatch" 	: 	call saveBatch
	case else 			: 	call index
end select

call db.close()

function index()
	sql = "select id,linkName,linkAddr,linkNumber from cj_link Order By id Desc"
	temp = db.arrayData(sql, page, rows)
	infoTotal = temp(0)
	data = temp(1)
	%><!--#include file="./view/link.index.html"--><%
end function

function edit()
	data = array("", siteUrl, 1)
	if iid > 0 then
		set rs = db.exec("select linkName,linkAddr,linkNumber from cj_link where id=" & iid, 1)
		if rs.eof then admin.message "站内连接ID不存在！"
		data = rs
		set rs = nothing
	end if
	%><!--#include file="./view/link.edit.html"--><%
end function

function save()
	dim linkName, linkAddr, linkNumber
	linkName = admin.filter(request.form("linkName"))
	linkAddr = trim(request.form("linkAddr"))
	linkNumber = app.int(request.form("linkNumber"))
	if len(linkName) < 1 then admin.message "站内连接名称不能为空！"
	if len(linkAddr) < 1 then admin.message "连接地址不能为空！"
	if linkNumber < -1 or linkNumber = 0 then admin.message "替换次数只能为整数或者-1"
	if iid > 0 then
		set rs = db.exec("select * from cj_link where id=" & iid, 3)
		if rs.eof then admin.message "站内连接ID" & iid & "不存在！"
	else
		set rs = db.exec("select * from cj_link", 3)
		rs.AddNew
	end if
	rs("linkName") = linkName
	rs("linkAddr") = linkAddr
	rs("linkNumber") = linkNumber
	rs.update
	rs.close : set rs = nothing
	app.appCacheDelete "innerLinks"
	if iid > 0 then
		admin.back "修改成功！", "返回列表", "?q=index"
	else
		admin.back "添加成功！", "继续添加", "?q=edit"
	end if
end function

function delete()
	call db.exec("Delete from cj_link where id in(" & batchId & ")", 1)
	app.appCacheDelete "innerLinks"
	app.json "删除成功！", 0
end function

function editBatch()
	%><!--#include file="./view/link.editBatch.html"--><%
end function

function saveBatch()
	dim linkNumber, islinkNumber, linkAddr, isLinkAddr
	islinkNumber = admin.isCheck(request.form("islinkNumber"))
	isLinkAddr = admin.isCheck(request.form("isLinkAddr"))
	if not(islinkNumber) and not(isLinkAddr) then admin.message "需要选择一项！"
	linkNumber = app.int(request.form("linkNumber"))
	linkAddr = trim(request.form("linkAddr"))
	set rs = db.exec("select * from cj_link where id in(" & batchId & ")", 3)
	do until rs.eof
		if islinkNumber then
		    rs("linkNumber") = linkNumber
		end if
		if isLinkAddr then
		    rs("linkAddr") = linkAddr
		end if
		rs.update
		rs.movenext
	loop
	app.appCacheDelete "innerLinks"
	rs.close : set rs = nothing
	admin.back "修改成功！", "返回列表", "?q=index"
end function

%>